<script setup>
import { onMounted, onUnmounted, watch } from 'vue'

const props = defineProps({
  data: Array,
  header: String,
  width: String,
  fontSize: String,
  scroll: {
    type: Boolean,
    default: false,
  },
  height: {
    type: String,
    default: '100%',
  },
  offset: {
    type: [Number, String],
    default: 0,
  },
})
const tableData = ref([])
const isScroll = ref(props.scroll)
watch(
  () => props.data,
  (newData) => {
    if (props.scroll) {
      tableData.value = newData
      tableData.value = [...tableData.value, ...tableData.value]
    } else {
      tableData.value = newData
    }
  },
  { immediate: true },
)
const timer = ref(null)
onMounted(() => {
  if (!props.scroll) return
  timer.value = setInterval(() => {
    if (isScroll.value) {
      tbody.value.scrollTop += 1 //设置滚动速度
      if (
        tbody.value.clientHeight + tbody.value.scrollTop >=
        tbody.value.scrollHeight - props.offset
      ) {
        tbody.value.scrollTop = 0
      }
    }
  }, 30)
})
onUnmounted(() => {
  clearInterval(timer.value)
  timer.value = null
})
const tbody = ref(null)
</script>
<template>
  <table>
    <thead>
      <tr class="tr" :style="{ 'grid-template-columns': width }">
        <th
          class="th"
          v-for="item in header.split(',')"
          :key="item"
          :style="{ fontSize: fontSize }"
        >
          {{ item }}
        </th>
      </tr>
    </thead>
    <tbody
      ref="tbody"
      :style="{ maxHeight: height }"
      @mouseover="isScroll = false"
      @mouseout="isScroll = true"
      class="auto-scroll-container"
    >
      <tr class="tr" v-for="item in tableData" :style="{ 'grid-template-columns': width }">
        <td class="td" v-for="key in Object.keys(item)" :key="key" :style="{ fontSize: fontSize }">
          {{ item[key] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>
<style>
.btn-prev,
.btn-next {
  background-color: transparent !important;
  color: #0682c9 !important;
}
.el-pager li {
  background-color: #084384 !important;
  color: #fff !important;
  border: 2px solid #065d91 !important;
}
.el-pager li.is-active {
  background-color: #00a6ff !important;
}
</style>
<style lang="scss">
table {
  margin: 0px auto;
  border-spacing: 0;
  height: 90%;
  width: 100%;
  overflow: hidden;
  .th {
    height: 40px;
    text-align: center;
    color: #fff;
  }
  .tr {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    height: 40px;
    line-height: 40px;
    position: relative;
  }
  .td {
    height: 40px;
    text-align: center;
    color: #fff;
    border-bottom: 2px solid #113953;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  thead {
    .th {
      color: #62c7f2;
    }
  }
  tbody {
    overflow-y: auto;
    display: block;
    .tr {
      &::after {
        content: '';
        display: block;
        background-image: url('@/assets/images/home/left-bottom/tr-left.png');
        background-size: contain;
        background-repeat: no-repeat;
        background-position: 0 0;
        width: 12px;
        height: 16px;
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
      }
      &::before {
        content: '';
        display: block;
        background-image: url('@/assets/images/home/left-bottom/tr-right.png');
        background-size: contain;
        background-repeat: no-repeat;
        background-position: 0 0;
        width: 12px;
        height: 16px;
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
      }
      &:nth-child(odd) {
        background: #006da726;
      }
      &:last-child {
        td {
          border-bottom: none;
        }
      }
    }
  }
  .auto-scroll-container {
    scrollbar-width: none;
    -ms-overflow-style: none;

    &::-webkit-scrollbar {
      display: none;
    }
  }
}
</style>
